package com.manelrodriguez.applications.ibex35.adapters;

import java.text.NumberFormat;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.manelrodriguez.applications.ibex35.R;
import com.manelrodriguez.applications.ibex35.activity.Ibex35Activity;
import com.manelrodriguez.applications.ibex35.to.Empresa;
import com.manelrodriguez.applications.ibex35.to.Movimiento;

public class CarteraListAdapter extends ArrayAdapter<Movimiento> {
	private int layoutResourceId;
	TextView tv_1;
	TextView tv_2;
	TextView tv_3;
	TextView tv_4;
	TextView tv_5;
	NumberFormat numberFormat;
	List<Movimiento> listaMovimientos;
	
	public CarteraListAdapter(Context context, int layoutResourceId, List<Movimiento> items){
		super(context, layoutResourceId, items);
		listaMovimientos = items;
        this.layoutResourceId = layoutResourceId;
        numberFormat = NumberFormat.getInstance();
		numberFormat.setMaximumFractionDigits(2);
	}
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
		RelativeLayout itemView;
		Movimiento mov = getItem(position);
		if(convertView==null)
		{
			itemView = new RelativeLayout(getContext());
			LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			vi.inflate(layoutResourceId, itemView, true);
		}else{
			itemView = (RelativeLayout)convertView;
		}
		
		tv_1 = (TextView)itemView.findViewById(R.id.cartera_id_empresa_r);
		tv_2 = (TextView)itemView.findViewById(R.id.cartera_num_acciones_r);
		tv_3 = (TextView)itemView.findViewById(R.id.cartera_importe_r);
		tv_4 = (TextView)itemView.findViewById(R.id.cartera_valor_actual_r);
		tv_5 = (TextView)itemView.findViewById(R.id.cartera_diferencia_r);
		
		if(!mov.getIdEmpresa().equals("")){
			Empresa emp = Ibex35Activity.hash_empresas.get(mov.getIdEmpresa());
			double precio = Double.valueOf(emp.getLastTradeAmount());
			double valorActual = precio * mov.getNumAcciones();
			double diferencia = valorActual-mov.getPrecioCompra();
			
			tv_1.setText(mov.getIdEmpresa());
			tv_2.setText(String.valueOf(numberFormat.format(mov.getNumAcciones())));
			tv_3.setText(String.valueOf(numberFormat.format(mov.getPrecioCompra())));
			tv_4.setText(String.valueOf(numberFormat.format(valorActual)));
			tv_5.setText(String.valueOf(numberFormat.format(diferencia)));
			if(diferencia>0){
				tv_5.setTextColor(Color.rgb(34, 177, 76));
			}else if(diferencia <0){
				tv_5.setTextColor(Color.RED);
			}
		}else{
			tv_1.setText(getContext().getString(R.string.total));
			tv_2.setText(String.valueOf(numberFormat.format(mov.getNumAcciones())));
			tv_3.setText(String.valueOf(numberFormat.format(mov.getPrecioCompra())));
			tv_4.setText(String.valueOf(numberFormat.format(getValorActualTotal())));
			double diferenciaTotal = getDiferenciaTotal();
			tv_5.setText(String.valueOf(numberFormat.format(diferenciaTotal)));
			if(diferenciaTotal>0){
				tv_5.setTextColor(Color.rgb(34, 177, 76));
			}else if(diferenciaTotal <0){
				tv_5.setTextColor(Color.RED);
			}
		}
		return itemView;
	}
	
	private double getValorActualTotal(){
		double total = 0;
		
		if(!listaMovimientos.isEmpty()){
			for(Movimiento mov:listaMovimientos){
				if(!mov.getIdEmpresa().equals("")){
					Empresa emp = Ibex35Activity.hash_empresas.get(mov.getIdEmpresa());
					double precio = Double.valueOf(emp.getLastTradeAmount());
					double valorActual = precio * mov.getNumAcciones();
					total += valorActual;
				}
			}
		}
		
		return total;
	}
	
	private double getDiferenciaTotal(){
		double total = 0;
		
		if(!listaMovimientos.isEmpty()){
			for(Movimiento mov:listaMovimientos){
				if(!mov.getIdEmpresa().equals("")){
					Empresa emp = Ibex35Activity.hash_empresas.get(mov.getIdEmpresa());
					double precio = Double.valueOf(emp.getLastTradeAmount());
					double valorActual = precio * mov.getNumAcciones();
					double diferencia = valorActual-mov.getPrecioCompra();
					total += diferencia;
				}
			}
		}
		
		return total;
	}
}
